package com.sxmmall.product.service.impl;

import com.sxmmall.product.entity.AttrEntity;
import com.sxmmall.product.service.AttrService;
import com.sxmmall.product.vo.AttrGroupWithAttrsVo;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sxmmall.common.utils.PageUtils;
import com.sxmmall.common.utils.Query;

import com.sxmmall.product.dao.AttrGroupDao;
import com.sxmmall.product.entity.AttrGroupEntity;
import com.sxmmall.product.service.AttrGroupService;


@Service("attrGroupService")
public class AttrGroupServiceImpl extends ServiceImpl<AttrGroupDao, AttrGroupEntity> implements AttrGroupService {

    @Autowired
    private AttrService attrService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AttrGroupEntity> page = this.page(
                new Query<AttrGroupEntity>().getPage(params),
                new QueryWrapper<AttrGroupEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public PageUtils queryPage(Map<String, Object> params, Long catalogId) {
        String key = (String) params.get("key");
        QueryWrapper<AttrGroupEntity> query = new QueryWrapper<>();

        if (!StringUtils.isEmpty(key)){
            query.and((obj)->{
                obj.like("attr_group_id",key).or().like("attr_group_name",key);
            });
        }
        if (catalogId == 0) {
            IPage<AttrGroupEntity> page = this.page(
                    new Query<AttrGroupEntity>().getPage(params),
                    query);

            return new PageUtils(page);
        } else {
            query.eq("catelog_id",catalogId);
            IPage<AttrGroupEntity> page = this.page(new Query<AttrGroupEntity>().getPage(params),
                    query);
            return new PageUtils(page);
        }
    }

    /**
     * 更具分类ID查出所有的分组  组里的属性
     * @param catelogId
     * @return
     */
    @Override
    public List<AttrGroupWithAttrsVo> getAttrGroupWithAttrsByCatelogId(Long catelogId) {
        // 1.查询分组信息
        List<AttrGroupEntity> attrGroupEntityList = this.list(new QueryWrapper<AttrGroupEntity>().eq("catelog_id", catelogId));
        List<AttrGroupWithAttrsVo> voList = attrGroupEntityList.stream()
                .map(item -> {
                    AttrGroupWithAttrsVo attrGroupWithAttrsVo = new AttrGroupWithAttrsVo();
                    BeanUtils.copyProperties(item,attrGroupWithAttrsVo);
                    List<AttrEntity> relationAttr = attrService.getRelationAttr(item.getAttrGroupId());
                    attrGroupWithAttrsVo.setAttrs(relationAttr);
                    return attrGroupWithAttrsVo;
                }).collect(Collectors.toList());
        return voList;
    }
}